<template>
  <div class="app-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="如若不适应手机端操作，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="table-container">
      <div style="padding-bottom:20px; display: flex;">
        <div style="width: 200px;">
          <el-input v-model="search" size="medium" placeholder="输入关键字搜索" />
        </div>
        <div style="width: 220px; margin-left: 20px;">
          <label>申请状态</label>
          <el-select v-model="status1" size="medium" placeholder="请选择" style="width: 160px;" @change="statusChange">
            <el-option label="待校审核" value="待校审核" />
            <el-option label="被校级退回" value="被校级退回" />
            <el-option label="审核通过" value="审核通过" />
          </el-select>
        </div>
      </div>
      <el-table
        v-loading="paperListLoading"
        :data="paperList"
        element-loading-text="Loading"
        border
        fit
      >
        <el-table-column type="selection" align="center" width="55" />
        <!-- <el-table-column align="center" label="序号" width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="项目名称" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.projectName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目编号" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.projectId }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目类别" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.projectCate }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目来源单位" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.projectUnit }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目负责人" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.head }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="到校经费（万元）" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.arrivingFunds || '0' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目状态" min-width="120">
          <template slot-scope="scope">
            <el-tag :type="scope.row.projectState | statusFilter">{{ scope.row.projectState }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="申请状态" min-width="120">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
            <el-tag v-if="scope.row.status === '被院级退回' | scope.row.status === '被校级退回'" type="warning">{{ scope.row.reason }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-popover
              :ref="`popoverpass-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认通过吗？该操作不可撤销</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popoverpass-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="thesis_pass(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-button
              size="mini"
              @click="open_infoview_dialog(scope.row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="thesis_back(scope.row)"
            >
              退回
            </el-button>
            <el-button
              v-popover="`popoverpass-${scope.$index}`"
              size="mini"
              type="success"
            >
              通过
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :current-page="p1_page"
          :page-sizes="[1,2,3,10, 20, 30, 40]"
          :page-size="p1_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="p1_total"
          @size-change="p1_sizechange"
          @current-change="p1_currentchange"
        />
        <!-- @size-change="handleSizeChange"
          @current-change="handleCurrentChange" -->
        <!-- 使用了size-change和current-change事件来处理页码大小和当前页变动时候触发的事件。 -->
      </div>
      <el-dialog title="查看信息" :visible.sync="infoview_dialog" width="75%">
        <el-collapse v-model="activeNames">
          <el-collapse-item name="1">
            <template slot="title">
              <div class="el-descriptions__title">项目信息</div>
            </template>
            <el-descriptions :column="2" size="medium" border :label-style="{width:'25%'}" :content-style="{width:'25%'}">
              <el-descriptions-item label="横向项目ID">
                {{ infoview1.tregistrationID }}
              </el-descriptions-item>
              <el-descriptions-item label="录入人">
                {{ infoview1.entered }}
              </el-descriptions-item>
              <el-descriptions-item label="录入日期">
                {{ infoview1.enteredDate }}
              </el-descriptions-item>
              <el-descriptions-item label="项目名称">
                {{ infoview1.projectName }}
              </el-descriptions-item>
              <el-descriptions-item label="项目编号">
                {{ infoview1.projectId }}
              </el-descriptions-item>
              <el-descriptions-item label="项目类别">
                {{ infoview1.projectCate }}
              </el-descriptions-item>
              <el-descriptions-item label="来源单位类型">
                {{ infoview1.sourceUnitType }}
              </el-descriptions-item>
              <el-descriptions-item v-if="infoview1.sourceUnitType === '国有企业' || infoview1.sourceUnitType === '民营企业'" label="来源企业/公司分类">
                {{ infoview1.sourceEnterpriseCategory }}
              </el-descriptions-item>
              <el-descriptions-item label="项目来源单位">
                {{ infoview1.projectUnit }}
              </el-descriptions-item>
              <el-descriptions-item label="来源单位/企业所在省、市">
                {{ infoview1.projectSourceUnitId }}
              </el-descriptions-item>
              <el-descriptions-item label="项目负责人">
                {{ infoview1.head }}
              </el-descriptions-item>
              <el-descriptions-item label="项目成员">
                {{ infoview1.projectMembers }}
              </el-descriptions-item>
              <el-descriptions-item label="项目状态">
                <el-tag :type="infoview1.projectState | statusFilter">{{ infoview1.projectState }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="项目开始日期">
                {{ infoview1.startTime }}
              </el-descriptions-item>
              <el-descriptions-item label="项目结束日期">
                {{ infoview1.endTime }}
              </el-descriptions-item>
              <el-descriptions-item label="立项日期">
                {{ infoview1.approvalTime }}
              </el-descriptions-item>
              <el-descriptions-item label="立项年度">
                {{ infoview1.approvalYear }}
              </el-descriptions-item>
              <el-descriptions-item label="档案编号">
                {{ infoview1.archiveNum }}
              </el-descriptions-item>
              <el-descriptions-item label="财务项目号">
                {{ infoview1.archiveNum }}
              </el-descriptions-item>
              <el-descriptions-item label="到校经费(万元)">
                {{ infoview1.arrivingFunds }}
              </el-descriptions-item>
              <el-descriptions-item label="合同经费(万元)">
                {{ infoview1.contractFunds }}
              </el-descriptions-item>
              <el-descriptions-item label="成果应用行业领域">
                {{ infoview1.nationalEconomyId }}
              </el-descriptions-item>
              <el-descriptions-item label="协作内容和要求">
                {{ infoview1.contentRequirements }}
              </el-descriptions-item>
              <el-descriptions-item label="预期成果(结题要求)">
                {{ infoview1.expect }}
              </el-descriptions-item>
              <el-descriptions-item label="预期目标和考核目标">
                {{ infoview1.goal }}
              </el-descriptions-item>
              <el-descriptions-item label="知识产权归属及分享">
                {{ infoview1.intellectual }}
              </el-descriptions-item>
              <el-descriptions-item label="工作进度计划">
                {{ infoview1.jobPlan }}
              </el-descriptions-item>
              <el-descriptions-item label="付款计划">
                {{ infoview1.payPlan }}
              </el-descriptions-item>
              <el-descriptions-item label="统计分类">
                {{ infoview1.classifi }}
              </el-descriptions-item>
              <el-descriptions-item label="承担单位">
                {{ infoview1.unit }}
              </el-descriptions-item>
              <el-descriptions-item label="出生年月">
                {{ infoview1.birthdate }}
              </el-descriptions-item>
              <el-descriptions-item label="职称">
                {{ infoview1.title }}
              </el-descriptions-item>
              <el-descriptions-item label="学历">
                {{ infoview1.education }}
              </el-descriptions-item>
              <el-descriptions-item label="申请状态">
                <el-tag :type="infoview1.status | statusFilter">{{ infoview1.status }}</el-tag>
              </el-descriptions-item>
              <el-descriptions-item label="退回原因">
                {{ infoview1.reason }}
              </el-descriptions-item>
              <el-descriptions-item label="备注">
                {{ infoview1.notes }}
              </el-descriptions-item>
            </el-descriptions>
          </el-collapse-item>
          <el-collapse-item name="2">
            <template slot="title">
              <div class="el-descriptions__title">成员列表</div>
            </template>
            <el-table
              v-loading="paperAuthorListLoading"
              :data="paperAuthorList"
              element-loading-text="Loading"
              border
              fit
            >
              <el-table-column type="index" align="center" label="序号" width="55" />
              <el-table-column align="center" label="姓名" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.name }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="所属院系" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.dept }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="署名单位" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.unit }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="排名" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.ranking }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="作者类型" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.authortype }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="作者类别" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.authorcate }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="考核系数" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.assessment }}
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
          <el-collapse-item name="3">
            <template slot="title">
              <div class="el-descriptions__title">附件信息</div>
            </template>
            <el-table
              v-loading="fileListLoading"
              :data="fileList"
              element-loading-text="Loading"
              border
              fit
            >
              <el-table-column type="index" align="center" label="序号" width="55" />
              <el-table-column align="left" label="文件名称" min-width="150">
                <template slot-scope="scope">
                  {{ scope.row.filename }}
                </template>
              </el-table-column>
              <el-table-column align="left" label="文件大小" min-width="150">
                <template slot-scope="scope">
                  <span v-if="scope.row.filesize / 1024 / 1024 < 1">{{ (scope.row.filesize / 1024).toFixed(2) + 'KB' }}</span>
                  <span v-else>{{ (scope.row.filesize / 1024 / 1024).toFixed(2) + 'MB' }}</span>
                </template>
              </el-table-column>
              <el-table-column align="left" label="上传时间" min-width="150">
                <template slot-scope="scope">
                  {{ scope.row.filedate }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="操作" width="150">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="downloadFile(scope.row)"
                  >
                    下载
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-collapse-item>
        </el-collapse>
        <div slot="footer" class="dialog-footer">
          <el-button @click="infoview_dialog = false">关 闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { get_thesis_xiaoji, thesis_back_xiaoji1, thesis_pass_xiaoji1, get_thesis_author, get_file } from '@/api/horizontal'
import { Message } from 'element-ui'
import { mapGetters } from 'vuex'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        草稿: 'info',
        待院审核: '',
        被院级退回: 'danger',
        待校审核: '',
        被校级退回: 'danger',
        审核通过: 'success',
        在研: 'success',
        结题: 'success',
        在研待院审核: '',
        在研待校审核: '',
        结题待院审核: '',
        结题待校审核: '',
        在研退回: 'danger',
        结题退回: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: '',
      thesis_add_dialog: false,
      thesis_info_dialog: false,
      infoview_dialog: false,
      visible1: false,
      visible2: false,
      paperList: null,
      paperListLoading: false,
      p1_size: 10,
      p1_total: 0,
      p1_page: 1,
      status1: '待校审核',
      status2: '',
      back_post: {
        id: '',
        reason: ''
      },
      paperAuthorList: [],
      paperAuthorListLoading: false,
      infoview1: {
        approvalMaterial: '',
        approvalTime: '',
        approvalYear: '',
        archiveNum: '',
        arrivingFunds: '',
        birthdate: '',
        classifi: '',
        contentRequirements: '',
        contractFunds: '',
        endTime: '',
        entered: '',
        enteredDate: '',
        expect: '',
        financialNum: '',
        goal: '',
        head: '',
        intellectual: '',
        jobPlan: '',
        notes: '',
        payPlan: '',
        projectCate: '',
        projectId: '',
        projectMembers: '',
        projectName: '',
        projectState: '',
        projectUnit: '',
        reason: '',
        startTime: '',
        status: '',
        title: '',
        tregistrationID: '',
        unit: ''
      },
      activeNames: ['1', '2', '3'],
      fileList: [],
      fileListLoading: false
    }
  },
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ]),
    MyPaperPost() {
      return {
        name: this.name,
        pageNum: this.p1_page,
        pageSize: this.p1_size,
        status1: this.status1,
        status2: this.status2
      }
    }
  },
  created() {
    this.fetchPaperData()
  },
  methods: {
    fetchPaperData() {
      this.paperListLoading = true
      get_thesis_xiaoji(this.MyPaperPost).then(response => {
        if (response.code === 200) {
          this.paperList = response.data.list
          this.p1_total = response.data.total
          this.paperListLoading = false
        }
      })
    },
    statusChange() {
      this.fetchPaperData()
    },
    p1_sizechange(newsize) {
      this.p1_size = newsize
      this.p1_page = 1
      this.fetchPaperData()
    },
    p1_currentchange(newpage) {
      this.p1_page = newpage
      this.fetchPaperData()
    },
    filterTag(value, row) {
      return row.status === value
    },
    thesis_back(row) {
      this.$prompt('请输入退回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.+$/,
        inputErrorMessage: '原因不能为空'
      }).then(({ value }) => {
        this.back_post.id = row.tregistrationID
        this.back_post.reason = value
        thesis_back_xiaoji1(this.back_post).then(response => {
          if (response.code === 200) {
            Message.success('退回成功')
            this.fetchPaperData()
          }
        })
      }).catch(() => {
        Message.info('取消退回')
      })
    },
    thesis_pass(index, row) {
      this.$refs[`popoverpass-${index}`].doClose()
      thesis_pass_xiaoji1(row.tregistrationID).then(response => {
        if (response.code === 200) {
          Message.success('通过成功')
          this.fetchPaperData()
        }
      })
    },
    fetchOneData() {
      get_thesis_xiaoji(this.infoview1.tregistrationID).then(response => {
        if (response.code === 200) {
          this.infoview1 = response.data
          // 字段映射
          this.infoview1.organization = response.data.deptName
          this.infoview1.entered = response.data.enteredName
          // 项目详情加载完成后，获取并过滤成员列表
          this.paperAuthorListLoading = true
          get_thesis_author(this.infoview1.tregistrationID).then(authorResponse => {
            if (authorResponse.code === 200) {
              // 显示所有作者，包括项目负责人
              this.paperAuthorList = authorResponse.data
              this.paperAuthorListLoading = false
            }
          })
          this.fileListLoading = true
          get_file(this.infoview1.tregistrationID).then(fileResponse => {
            if (fileResponse.code === 200) {
              this.fileList = fileResponse.data
              this.fileListLoading = false
            }
          })
        }
      })
    },
    open_infoview_dialog(row) {
      this.infoview_dialog = true
      this.infoview1 = JSON.parse(JSON.stringify(row))
      this.fetchOneData()
    },
    downloadFile(row) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.href = row.fileurl
      a.target = '_blank'
      a.dispatchEvent(event)
    }
  }
}
</script>

<style lang="scss">
.el-form{
  margin-left: 40px;
  .el-input,.el-select{
    width: 300px;
  }
}
.el-tag + .el-tag {
  margin: 10px 10px;
}
.margin-top {
  margin-top: 50px;
}
@media screen and (max-width:600px) {
  .el-dialog{
    width: 100% !important;
  }
  .el-form{
    margin-left: 25px;
  }
}
</style>
